package com.xneure.code.exception;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xneure.utils.JsonUtil;
import com.xneure.utils.ObjectUtil;
import com.xneure.utils.SpringUtil;

public class GExceptionHandler{
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public Object handler(HttpServletRequest req,HttpServletResponse response,Exception e){
		String[] es = SpringUtil.getBeanName(SpringException.class);
		if(ObjectUtil.isNotNull(es)){
			StringBuilder sb = new StringBuilder();
			for(String b:es){
				sb.append(JsonUtil.toJson(((SpringException)SpringUtil.getBean(b)).handler(req,response,e)));
				sb.append("\r\n");
			}
			return sb.toString();
		}else {
			e.printStackTrace();
			try{
				response.setStatus(400);
				SpringUtil.returnString("操作发生错误。");
			}catch(IOException e1){
			}
			return null;
		}
	}
}
